home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.04 Aug 92 / Hello TCL World / AuxWindow.c next >
Encoding:
C/C++ Source or Header  |  1991-05-20  |  8.5 KB  |  382 lines  |  [TEXT/KAHL]

  1. /*                        AuxWindow.c                        */
  2. /*
  3.  * AuxWindow
  4.  *    Superclass: CDirector.
  5.  * Copyright © 1991 Martin Minow. All Rights Reserved.
  6.  * All use and non-commercial distribution permitted.
  7.  * Set tabs every 4 bytes.
  8.  *
  9.  * This class implements a generic auxiliary window that
  10.  * may be used in addition to the document's normal
  11.  * window.  It is based on the CClipboard class.
  12.  */
  13.  
  14. #ifdef DOCUMENTATION
  15.  
  16. Title        AuxWindow
  17. Superclass    CDirector
  18. Subclasses    TextWindow
  19.  
  20. usage
  21.  
  22.     #include    "AuxWindow.h"
  23.     
  24.     void                    IAuxWindow(
  25.         CApplication    *aSupervisor,
  26.         short            aWindowId,
  27.         Boolean            isFloating,
  28.         Boolean            hideOnSuspend,
  29.         long            toggleCmd,
  30.         short            showHideRes
  31.     );
  32.         Initialization.  itsSupervisor must be the
  33.         application.  It displays the window defined
  34.         by aWindowId.  See the description of CWindow
  35.         for information on floating windows.
  36.         
  37.         hideOnSuspend is TRUE if this window should follow
  38.         the Macintosh human interface guidelines, which
  39.         recommend that floating windows and general status
  40.         windows be hidden when your application is suspended.
  41.         Set it FALSE if this window should remain visible
  42.         except when the user explicitly hides it via the
  43.         menu command (Toggle method) or by clicking in
  44.         the close box.
  45.         
  46.         toggleCmd is the command number your application
  47.         uses to make the window visible and invisible.
  48.         If you don't have a show/hide menu command, just
  49.         pass cmdNull and set showHideRes to zero. Otherwise,
  50.         your application class should create a menu item
  51.         using some variant on the following sequence:
  52.         
  53.           Str255            work;
  54.           short                MENUid;
  55.           MenuHandle        macMenu;
  56.           short                itemNo;
  57.           
  58.           GetIndString(work, itsMenuNames, kShowAuxWindow);
  59.           gBartender->FindMenuItem(
  60.             cmdToggleClip,
  61.             &MENUid,
  62.             &macMenu,
  63.             &itemNo
  64.           );
  65.           if (macMenu != NULL && work[0] != 0) {
  66.             gBartender->InsertMenuCmd(
  67.               CmdToggleAuxWindow,
  68.               work,
  69.               MENUid,
  70.               itemNo
  71.             );
  72.           }
  73.  
  74.         The above sequence creates a menu item that resides
  75.         just below the Toggle Clipboard menu item. You can
  76.         also just add the menu item to your resource file.
  77.           
  78.         showHideRes identifies a STR# resource containing:
  79.             Show Status Window
  80.             Hide Status Window
  81.         
  82.         These menu items will be added to the menu bar
  83.         just after the Show/Hide clipboard menu command.
  84.         
  85.         Your application class DoCommand method should
  86.         recognize the toggleCmd and send a Toggle message
  87.         to the TextWindow:
  88.         
  89.             MyApp::DoCommand(
  90.                     long            theCommand
  91.                 )
  92.             {
  93.                 switch (theCommand) {
  94.                 case CmdToggleAuxWindow:
  95.                     myAuxWindow->Toggle();
  96.                     break;
  97.                 ...
  98.             }
  99.         
  100.         Your application class must also enable the
  101.         toggle command it its UpdateMenus method:
  102.         
  103.             MyApp::UpdateMenus()
  104.             {
  105.                 inherited::UpdateMenus();
  106.                 gBartender->EnableCmd(CmdToggleAuxWindow);
  107.             }
  108.         
  109.     void                    Dispose(void);
  110.         Dispose of the window. Actually, AuxWindow doesn't
  111.         have its own Dispose method: the CDirector
  112.         Dispose will free the visual hierarchy and other
  113.         local structures.
  114.         
  115.     void                    SetTitle(
  116.         StringPtr        aString
  117.     );
  118.         Set the AuxWindow title:
  119.             myAuxWindow->SetTitle(macSFReply->fName);
  120.  
  121.     void                    Show(void);
  122.     void                    Hide(void);
  123.     void                    Toggle(void);
  124.         Your application calls Toggle() to show or hide
  125.         the window.  By default, the window is created
  126.         invisibly.  Show() and Hide() explicitly set the
  127.         window's visibility.
  128.         
  129. Support Classes
  130.  
  131.     void                    Suspend(void);
  132.     void                    Resume(void);
  133.         These override the default classes.  They hide
  134.         when the application is suspended, and show it
  135.         when the application is resumed (assuming it
  136.         was visible, of course and hideOnSuspend was
  137.         set when the window was created).
  138.     
  139.     void                    Close(
  140.         Boolean            quitting
  141.     );
  142.     void                    CloseWind(
  143.         CWindow            *theWindow
  144.     );
  145.         These override the standard classes so the window
  146.         is hidden when closed, rather than deleted.
  147.         
  148.     
  149. window definition
  150.  
  151.     The window is defined by resources as follows:
  152.             
  153.     *
  154.     * The AuxWindow Window
  155.     *
  156.     Type WIND
  157.     Text Box, 1024                ;; WIND_AuxWindow
  158.  
  159.      40 40 236 361
  160.     InVisible GoAway
  161.     4                            ;; noGrowDocProc
  162.     0
  163.     
  164.     *
  165.     * The AuxWindow menu commands
  166.     *
  167.     Type STR#
  168.     Text Box, 1024                ;; STRS_AuxWindow
  169.     2
  170.     Show Status Window
  171.     Hide Status Window
  172.     
  173.     
  174. Author
  175.     Martin Minow
  176.  
  177. Copyright
  178.     Copyright © 1991 Martin Minow. All Rights Reserved.
  179.     Non-commercial use and distribution permitted.
  180.     
  181. #endif
  182.  
  183. #include <CBartender.h>
  184. #include <CDesktop.h>
  185. #include <Commands.h>
  186. #include "AuxWindow.h"
  187.  
  188. extern CBartender        *gBartender;
  189. extern CBureaucrat        *gGopher;
  190. extern CDesktop            *gDesktop;
  191. extern Boolean            gInBackground;
  192.  
  193. /*
  194.  * Initialize the AuxWindow.  A director consists of a
  195.  * visual hierarchy (CWindow and CPanes) along with a
  196.  * control hierarchy. In this case, there really isn't
  197.  * a unique control hierarchy: all of the standard "move
  198.  * window" operations are handled by the TCL classes. All
  199.  * we need to do here is to create the window and data
  200.  * panes. In order to support hiding and revealing the
  201.  * window, the application needs a Toggle Status Window
  202.  * command and a STR# resource with two strings:
  203.  *        Show Status Window
  204.  *        Hide Status Window
  205.  */
  206. void
  207. AuxWindow::IAuxWindow(
  208.         CApplication        *aSupervisor,
  209.         short                aWindowId,
  210.         Boolean                isFloating,
  211.         Boolean                isHiddenOnSuspend,
  212.         long                toggleCmd,    /* Our command    */
  213.         short                showHideRes    /* STR# text    */
  214.     )
  215. {
  216.         /*
  217.          * Initialize the superclass.
  218.          */
  219.         CDirector::IDirector(aSupervisor);
  220.         /*
  221.          * Next, create and position the window.
  222.          */
  223.         itsWindow = new(CWindow);
  224.         itsWindow->IWindow(
  225.             aWindowId,                /* A noGrowDocProc    */
  226.             isFloating,                /* Maybe floating    */
  227.             gDesktop,
  228.             this
  229.         );
  230.         hideOnSuspend = isHiddenOnSuspend;
  231.         itsToggleCmd = toggleCmd;
  232.         itsMenuNames = showHideRes;
  233.         windowVisible = FALSE;
  234. #if 0
  235.         /*
  236.          * Your sub-class may want to position the
  237.          * window and create it visibly.  If so,
  238.          * add the following (or whatever is appropriate)
  239.          * to your initialization method.
  240.          */
  241.         gDecorator->CenterWindow(itsWindow);
  242.         Show();
  243. #endif
  244. }
  245.  
  246. /*
  247.  * The SetTitle method sets the window's title bar.  Note
  248.  * that an application that uses the AuxWindow dosen't know
  249.  * that the title is going to a window -- or even that
  250.  * there is a window. I.e. you could easily turn the
  251.  * AuxWindow class into a printing or file-logging class.
  252.  */
  253. void
  254. AuxWindow::SetTitle(
  255.         StringPtr        aTitle
  256.     )
  257. {
  258.         itsWindow->SetTitle(aTitle);
  259. }
  260.  
  261. /*
  262.  * The Suspend, Resume, Close, and CloseWind methods
  263.  * override their CDirector counterparts in order to
  264.  * permit hiding and revealing the AuxWindow window
  265.  * without destroying it. Your application (or some other
  266.  * class) must respond to the CmdToggle command by calling
  267.  * the Toggle() method. The original idea for this was
  268.  * taken from the TCL CClipboard class.
  269.  */
  270.  
  271. void
  272. AuxWindow::Suspend()
  273. {
  274.         if (windowVisible) {
  275.             if (hideOnSuspend == FALSE)
  276.                 itsWindow->HideSuspend();
  277.             if (active) {
  278.                 itsWindow->Deactivate();
  279.                 active = TRUE;
  280.             }
  281.         }
  282. }
  283.  
  284. void
  285. AuxWindow::Resume()
  286. {
  287.         if (windowVisible) {
  288.             if (hideOnSuspend == FALSE)
  289.                 itsWindow->ShowResume();
  290.             if (active) {
  291.                 itsWindow->Activate();
  292.                 gGopher = this;
  293.             }
  294.         }
  295. }
  296.  
  297. /*
  298.  * Close the AuxWindow by hiding it. We don't care if the
  299.  * application is quitting or not.
  300.  */
  301. Boolean
  302. AuxWindow::Close(
  303.         Boolean            quitting
  304.     )
  305. {
  306.         if (windowVisible) {
  307.             if (gInBackground)
  308.                 itsWindow->ShowResume();
  309.             CloseWind(itsWindow);
  310.         }
  311.         return (TRUE);
  312. }
  313.  
  314. /*
  315.  * The user (probably) clicked in the close box --
  316.  * or we're here because visiblity was toggled.
  317.  * just close the window and fix the menu.
  318.  */
  319. void
  320. AuxWindow::CloseWind(
  321.         CWindow            *theWindow
  322.     )
  323. {
  324.         Str255            work;
  325.         
  326.         theWindow->Hide();
  327.         windowVisible = FALSE;
  328.         Deactivate();
  329.         if (itsToggleCmd != cmdNull) {
  330.             GetIndString(
  331.                 work, itsMenuNames, kShowAuxWindow);
  332.             gBartender->SetCmdText(itsToggleCmd, work);
  333.         }
  334. }
  335.  
  336. void
  337. AuxWindow::Hide()
  338. {
  339.         if (windowVisible)
  340.             CloseWind(itsWindow);
  341. }
  342.  
  343. void
  344. AuxWindow::Show()
  345. {
  346.         Str255            work;
  347.         
  348.         if (windowVisible == FALSE) {
  349.             itsWindow->Select();
  350.             windowVisible = TRUE;
  351.             if (itsToggleCmd != cmdNull) {
  352.                 GetIndString(
  353.                     work, itsMenuNames, kHideAuxWindow);
  354.                 gBartender->SetCmdText(itsToggleCmd, work);
  355.             }
  356.             /*
  357.              * If the application enabled floating windows,
  358.              * this window will be created "deactivated"
  359.              * because an unwanted deactivate event is
  360.              * stuffed -- somehow -- into the event queue. 
  361.              * I don't really understand this, so don't be
  362.              * suprised if it turns out to be a bug.
  363.              */
  364.             if (CurDeactive == itsWindow->GetMacPort())
  365.                 CurDeactive = NULL;
  366.         }
  367. }
  368.  
  369. /*
  370.  * Toggle the window visiblity.  If it was invisible (or
  371.  * newly created), set the menu command.
  372.  */
  373. void
  374. AuxWindow::Toggle()
  375. {
  376.         if (windowVisible)
  377.             Hide();
  378.         else {
  379.             Show();
  380.         }
  381. }
  382.